	$(function(){
		$("#beginTime").datepicker();
		$("#endTime").datepicker();
	ajax_table();
});
//结算模式改变
function on_method_onchange(){
	$("#utable").bootstrapTable('destroy');
	ajax_table();
}

function ajax_table(type){
	var t_settlement_method= $("#t_settlement_method").val();
	var endTime= $("#endTime").val();
	console.info(t_settlement_method+"-"+beginTime+"-"+endTime);
	var condition_id=$("#condition_id").val();
	if(!condition_id){
		condition_id=0
	}
	if(condition_id<0){
		alert("渠道ID格式不正确");
	}
	  //生成用户数据
	   var url = path+"/admin/get-channel-data-list.htm";
	    $('#utable').bootstrapTable({
	        url: url,
	        method: 'post',
	        contentType: "application/x-www-form-urlencoded",
	        queryParamsType:'', 
	        queryParams: function (params){
	        	return {
	        		pageSize: params.limit,
	        		page:params.pageNumber,
	        		condition_id:condition_id,
	        		condition_name:$("#condition_name").val(),
	        		t_settlement_method:$("#t_settlement_method").val(),
	        		beginTime:$("#beginTime").val(),
	        		endTime:$("#endTime").val()
	        	}
	        }, 
	        //【其它设置】
	        locale:'zh-CN',//中文支持
	        pagination: true,//是否开启分页（*）
	        pageNumber:1,//初始化加载第一页，默认第一页
	        pageSize: 10,//每页的记录行数（*）
	        sidePagination: "server", //分页方式：client客户端分页，server服务端分页（*）
	        //【设置列】
	        columns: [
	                  {
	              		title:'渠道ID',
	              		field:'t_uid',
	              		align:'center',
	              		formatter:function(value,row){
	              			return value+10000;
	              		}
	              	},
	              	{
	              		title:'渠道名称',
	              		align:'center',
	              		field:'t_channel_name'
	              	},
	              	{
	              		title:'渠道类型',
	              		align:'center',
	              		field:'t_settlement_method',
	              		formatter:function(value,row){
	              			return value==0?'CPS':'CPA';
	              		}
	              	},
	             	{
	              		title:'统计时间',
	              		align:'center',
	              		field:'t_day'
	              	},
	              	{
	              		title:'注册量',
	              		align:'center',
	              		field:'t_install_amount'
	              	},
	              	{
	              		title:'苹果注册',
	              		align:'center',
	              		field:'t_user_iphone'
	              	},
	              	{
	              		title:'安卓注册',
	              		align:'center',
	              		field:'t_user_android'
	              	},
	              	
	              	{
	              		title:'有效注册量',
	              		align:'center',
	              		formatter:function(value,row,index){
	              			return row.t_cpa_iphone+row.t_cpa_android;
	              		}
	              			
	              	},
	              	{
	              		title:'有效苹果注册',
	              		align:'center',
	              		field:'t_cpa_iphone'
	              	},
	              	{
	              		title:'有效安卓注册',
	              		align:'center',
	              		field:'t_cpa_android'
	              	},
	              	
	              	{
	              		title:'CPA单价',
	              		align:'center',
	              		field:'t_channel_price'
	              	},
	             	{
	              		title:'VIP收益',
	              		align:'center',
	              		field:'t_vip_income'
	              	},
	            	{
	              		title:'金币充值收益',
	              		align:'center',
	              		field:'t_gold_income'
	              	},
	              	
	              	{
	              		title:'最高转化率',
	              		align:'center',
	              		formatter:function(value,row,index){
	              			return (((row.t_vip_income+row.t_gold_income+row.t_vip_income_two+row.t_gold_income_two)/3/row.t_install_amount)*100).toFixed(2)+'%';
	              		}
	              	},
	              	
	             	{
	              		title:'二级VIP收益',
	              		align:'center',
	              		field:'t_vip_income_two',
	              		formatter:function(value,row,index){
	              			if(row.t_settlement_method==1){
	              				return '0';
	              			}else{
	              				return value;
	              			}
	              		}
	              	},
	            	{
	              		title:'二级充值收益',
	              		align:'center',
	              		field:'t_gold_income_two',
	              		formatter:function(value,row,index){
	              			if(row.t_settlement_method==1){
	              				return '0';
	              			}else{
	              				return value;
	              			}
	              		}
	              	},
	            	{
	              		title:'收益',
	              		align:'center',
	              		formatter:function(value,row,index){
	              			if(row.t_settlement_method==0){
	              				//CPS
	              				return row.t_vip_income+row.t_gold_income+row.t_vip_income_two+row.t_gold_income_two;
	              			}else if (row.t_settlement_method==1){
	              				//CPA
	              				return (row.t_channel_price)*(row.t_cpa_iphone+row.t_cpa_android);
	              			}
	              		}
	              	},
	              	/*{
	              		title:'有效CPS分成',
	              		align:'center',
	              		field:'t_cps_money'
	              	},*/
	            	{
	              		title:'到账方式',
	              		align:'center',
	              		field:'t_settlement_type',
	              		formatter:function(value){
	              			console.log(value);
	              			return value == '0' ?'支付宝':value =='1' ?'微信':'银行卡';
	              		}
	              	},
	              	{
	              		title:'核对姓名',
	              		align:'center',
	              		field:'t_account_name' 
	              	},
	             	{
	              		title:'账号',
	              		align:'center',
	              		field:'t_bank_number' 
	              	},
	              	{
	              		title:'状态',
	              		align:'center',
	              		field:'t_settle_accounts',
	              		formatter:function(value){
	              			return value == 0?'未结算':'已结算';
	              		}
	              	},
	              	{
	              		title:'结算转化率',
	              		align:'center',
	              		field:'t_settlement_percent',
	              		formatter:function(value,row){
	              			//0:CPS 1:CPA
	              			if(row.t_settlement_method==0){
	              				return '';
	              			}
	              			
	              			var str;
	              			if(value==null){
	              				 str= "<input type='text' placeholder='CPS:填写 1-100 CPA:填写数字N 满N减1' id='t_settlement_percent_"+row.t_id+"'/> ";
	              			}else{
	              				 str= "<input type='text' value="+value+" placeholder='CPS:填写 1-100 CPA:填写数字N 满N减1' id='t_settlement_percent_"+row.t_id+"'/> ";
	              			}
	              			
	              			str=str+"<a href = 'javascript:on_click_to_percent("+row.t_id+")' class = 'btn btn-default' >确定</a>";
	              			return str
	              		}
	              	},
	              	{
	              		title:'操作',
	              		align:'center',
	              		formatter:function(value,row,index){
	              			var str = ""; 
	              			if(row.t_settle_accounts == 0){
	              				str = str + "<a href = 'javascript:on_click_settle_account("+row.t_id+")' class = 'btn btn-default' >结算</a>";
	              			}
	              			//0:CPS 1:CPA
	              			if(row.t_settlement_method==0){
	              				return str;
	              			}
	              			if(row.t_is_release == 0){
	              				str = str + "<a href = 'javascript:on_click_to_release("+row.t_id+",1)' class = 'btn btn-default' >发布</a>";
	              			}else  {
	              				str = str + "<a href = 'javascript:on_click_to_release("+row.t_id+",0)' class = 'btn btn-default' style='color:green'>取消发布</a>";
	              			}
	              			return str;
	              		}
	              	}
	        ]
	    });
}


//点击结算
function on_click_settle_account(id){
	$.ajax({
		type : 'POST',
		url : path + '/admin/settle-account-channel-data.htm',
		data : { t_id : id },
		dataType : 'json',
		success : function(data) {
			if (data.m_istatus == 1) {
				$("#myModal").modal('hide');
				$("#utable").bootstrapTable('refreshOptions',{page : 1});
			} else  {
				window.location.href = path + '/error.html';
			}
		}
	});
}

//点击发布on_click_to_prenct
function on_click_to_release(id,t_is_release){
	
	
	$.ajax({
		type : 'POST',
		url : path + '/admin/settle-to_release-data.htm',
		data : { t_id : id,
			t_is_release:t_is_release},
		dataType : 'json',
		success : function(data) {
			if (data.m_istatus == 1) {
				$("#myModal").modal('hide');
				$("#utable").bootstrapTable('refreshOptions',{page : 1});
			} else  {
				window.location.href = path + '/error.html';
			}
		}
	});
}
 
//点击发布
function on_click_to_percent(id){
	
	
	$.ajax({
		type : 'POST',
		url : path + '/admin/settle-to_percent-data.htm',
		data : { t_id : id,
			t_settlement_percent:$("#t_settlement_percent_"+id).val()},
		dataType : 'json',
		success : function(data) {
			if (data.m_istatus == 1) {
				$("#myModal").modal('hide');
				$("#utable").bootstrapTable('refreshOptions',{page : 1});
			} else  {
				window.location.href = path + '/error.html';
			}
		}
	});
}


